<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class News_model extends MY_Model
{

	public function getNewsList($data = array(),$per_page, $limit){
        
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND addtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND addtime <= {$data['endtime']} and onlinestatus=1 ";
        }
        if(!empty($data['keyword'])){
            $where .= " AND content like '%{$data['keyword']}%'";
        }
        if(!empty($data['cat_id'])){
            $where .= " AND categoryid = {$data['cat_id']}";
        }
         if($data['isruanwen'] != ""){
           $where .= " AND isruanwen = {$data['isruanwen']}";
        }
         if($data['readonly'] != ""){
           $where .= " AND readonly = {$data['readonly']}";
        }
        if(!empty($data['review_content'])){
            $where .= " AND r.content like '%{$data['review_content']}%'";
        }
        if(!empty($data['fromUsername'])){
            $where .= " AND r.fromUsername = '{$data['fromUsername']}'";
        }
        if(in_array($_SESSION['roleadmin'],[2,4]))
        {
            $where .= " AND operationid={$_SESSION['adminUserInfo']['id']}";
        }

        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT *  FROM blues_news  WHERE  isDel<>1  {$where}  ORDER BY addtime DESC {$limit} ";
        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


    public function getnewsCnt($data = array()){
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND addtime >= {$data['starttime']} and onlinestatus=1";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND addtime <= {$data['endtime']}";
        }
        if(!empty($data['keyword'])){
            $where .= " AND content like '%{$data['keyword']}%'";
        }
        if(!empty($data['cat_id'])){
            $where .= " AND categoryid ={$data['cat_id']}";
        }
        if(!empty($data['review_content'])){
            $where .= " AND r.content like '%{$data['review_content']}%'";
        }
        if($data['isruanwen'] != ""){
           $where .= " AND isruanwen = {$data['isruanwen']}";
        }
        if($data['readonly']  != ""){
           $where .= " AND readonly = {$data['readonly']}";
        }
        if(!empty($data['fromUsername'])){
            $where .= " AND r.fromUsername = '{$data['fromUsername']}'";
        }
        if(in_array($_SESSION['roleadmin'],[2,4]) )
        {
            $where .= " AND operationid={$_SESSION['adminUserInfo']['id']}";
        }
        
        $sql = "SELECT * FROM blues_news WHERE isDel=0  {$where}  ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }


     //编辑
    public function editNews($id,$data = array()){
        if($id){
            $this->db->where("newsID",$id);
            $res = $this->db->update("blues_news",$data);
        }else{
            $res = $this->db->insert("blues_news",$data);
        }
        return $res;
    }

    public function delNews($id){
        $this->db->where("newsID",$id);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("blues_news",$data);
        return $res;
    }

     //根据id获取广告信息
    public function getNewsInfobyID($id){
        $this->db->where("newsID",$id);
        $query = $this->db->get("blues_news");
        return $query->row_array();
    }


    public function editStatus($data = array()){

        $this->db->where("newsID",$data['newsID']);
        
        $updata['onlinestatus'] = $data['onlinestatus'];
		if($data['onlinestatus'] == 0)
		{
			$updata['ispublish'] = 0;
		}
       
        $res = $this->db->update("blues_news",$updata);
        return $res;
    }

    public function getExpireRuanwen()
    {
        $nowTime = time();
        $sql = "SELECT newsID, activity_endtime FROM blues_news WHERE isruanwen=1 AND onlinestatus=1 AND activity_endtime <= {$nowTime} AND activity_endtime<>0";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function xiajia($newsID)
    {
        $this->db->where("newsID",$newsID);
        $updata['onlinestatus'] = 0;
        $res = $this->db->update("blues_news",$updata);
        return $res;
    }
	public function getTotalMoneyByNewsID($newsID)
	{
		
		$sql = "SELECT sum(money) money FROM blues_member_money money ,blues_question ques WHERE ques.questionID=money.typeid AND  ques.newsID={$newsID} AND money.type=6";
		$query = $this->db->query($sql);
		return $query->row_array()['money'];
	}

}


